package pl.edu.agh.uddiProxy.parameter;

import pl.edu.agh.uddiProxy.types.AverageCounter;
import pl.edu.agh.uddiProxy.types.ParameterType;
import pl.edu.agh.uddiProxy.types.ParameterValue;


public class TimeAverageCounter implements AverageCounter<Double> {

	public void count(ParameterType parameterType,
			ParameterValue parameterValue, Double newValue) {
		parameterValue.setName(parameterType.getName());
		double v;
		if (parameterValue.getValue() == null) {
			v = 0;
		} else {
			v = Double.parseDouble(parameterValue.getValue().toString());
		}
		parameterValue.incrementNumberOfMeasurements();
		long count = parameterValue.getNumberOfMeasurements();
		
		v = ((v * (count - 1) + newValue) / (double) count);
		parameterValue.setValue(Double.toString(v));	
	}

}
